package com.ruijie.acl.service;

import com.ruijie.batch.plan.internal.DefaultItemParameterService;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.hibernate.ScrollableResults;
import org.springframework.util.Assert;

import com.ruijie.batch.plan.BatchPlan;
import com.ruijie.batch.plan.items.ItemParameters;
import com.ruijie.emp.components.deviceselector.TempDevice;

public class AclDefaultItemParameterService extends DefaultItemParameterService {

	@SuppressWarnings("unchecked")
	@Override
	public Collection resolveItemParameterses(BatchPlan plan, String pageSession) {
		Assert.hasText(pageSession);
		if (plan != null && plan.getId() != null) {
			ScrollableResults tempDeviceResults = this.createQuery(
					"from TempDevice as planItem " + "inner join fetch planItem.real as device "
							+ "left join fetch device.snmpTemplate "
							+ "left join fetch device.telnetTemplate "
							+ "left join fetch device.deviceMembers as deviceMembers "
							+ "left join fetch deviceMembers.slotInfos "
							+ "left join fetch device.systemGroup "
							+ "left join fetch device.deviceGroup "
							+ "left join fetch device.deviceModel as deviceModel "
							+ "left join fetch deviceModel.productIdSet "
							+ "left join fetch deviceModel.deviceType "
							+ "left join fetch deviceModel.deviceSeries as deviceSeriers "
							+ "left join fetch deviceSeriers.vendor "
							+ "where planItem.uuid = :pageSession " + "order by planItem.real.id ")
					.setParameter("pageSession", pageSession).scroll();

			ScrollableResults existsItemParamResults = this
					.createQuery(
							"select itemParam "
									+ "from ItemParameters as itemParam left join fetch itemParam.parameters, "
									+ "TempDevice as planItem, " + "Device as device "
									+ "where itemParam.plan.id = :plan "
									+ "and itemParam.item = planItem.real.id "
									+ "and planItem.uuid = :pageSession "
									+ "and planItem.real.id = device.id "
									+ "order by itemParam.item ")
					.setParameter("plan", plan.getId()).setParameter("pageSession", pageSession)
					.scroll();

			Collection<ItemParameters> existsItemParams = new ArrayList<ItemParameters>();
			try{
				while (existsItemParamResults.next()) {
					existsItemParams.add((ItemParameters) (existsItemParamResults.get())[0]);
				}
			}catch(Exception ex){
				//
			}

			Collection ret = new ArrayList();
			try{
				while (tempDeviceResults.next()) {
					Object[] entry = new Object[2];
					entry[0] = (tempDeviceResults.get())[0];
					entry[1] = null;
					Iterator iterator = existsItemParams.iterator();
					while (iterator.hasNext()) {
						ItemParameters itemParam = (ItemParameters) iterator.next();
						if (((TempDevice) entry[0]).getReal().getId().equals(itemParam.getItem())) {
							entry[1] = itemParam;
						}
					}
					ret.add(entry);
				}
			}catch(Exception ex){
				
			}
			
			return ret;
		} else {
			ScrollableResults tempDeviceResults = this.createQuery(
					"from TempDevice as planItem " + "inner join fetch planItem.real as device "
							+ "left join fetch device.snmpTemplate "
							+ "left join fetch device.telnetTemplate "
							+ "left join fetch device.deviceMembers as deviceMembers "
							+ "left join fetch deviceMembers.slotInfos "
							+ "left join fetch device.systemGroup "
							+ "left join fetch device.deviceGroup "
							+ "left join fetch device.deviceModel as deviceModel "
							+ "left join fetch deviceModel.productIdSet "
							+ "left join fetch deviceModel.deviceType "
							+ "left join fetch deviceModel.deviceSeries as deviceSeriers "
							+ "left join fetch deviceSeriers.vendor "
							+ "where planItem.uuid = :pageSession " + "order by planItem.real.id ")
					.setParameter("pageSession", pageSession).scroll();

			Collection ret = new ArrayList();
			while (tempDeviceResults.next()) {
				Object[] entry = new Object[2];
				entry[0] = (tempDeviceResults.get())[0];
				entry[1] = null;
				ret.add(entry);
			}
			return ret;
		}
	}

}
